package org.neuedu.dao;

import org.neuedu.bean.Article;
import org.neuedu.bean.reply;
import org.neuedu.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ReplyDaoImpl implements ReplyDao {
    @Override
    public int saveReplyInfo(reply reply) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into reply(aid,uid,replyContent) values (?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,reply.getAid());
            ps.setInt(2, reply.getUid());
            ps.setString(3,reply.getReplyContent());
            count = ps.executeUpdate();
            conn.commit();
        }catch (Exception e){
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;
    }

    @Override
    public int updateReplyNum(reply reply) {

        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            String sql = "update article set replynum = replynum + 1 where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,reply.getAid());
            count = ps.executeUpdate();
            conn.commit();
        }catch (Exception e){
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;

    }

    @Override
    public int deletecontentById(Integer id) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            String sql = "delete from reply where id=?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            count = ps.executeUpdate();
            conn.commit();
        }catch (Exception e){
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;

    }

    @Override
    public List<reply> getReplyByUid(Integer uid) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<reply> list = new ArrayList<>();
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select r.id id,r.aid aid,a.title title,r.uid uid,DATE_FORMAT(replyTime,'%Y-%m-%d %H:%i:%s') replytime,replyContent " +
                    "from reply r join article a on r.aid = a.id " +
                    "where r.uid =? and a.cid = 1" ;
            ps = conn.prepareStatement(sql);
            ps.setInt(1,uid);
            rs = ps.executeQuery();
            while (rs.next()) {
                reply reply = new reply();
                reply.setId(rs.getInt("id"));
                reply.setAid(rs.getInt("aid"));
                reply.setUid(rs.getInt("uid"));
                reply.setReplyTime(rs.getString("replytime"));
                reply.setReplyContent(rs.getString("replyContent"));
                Article article = new Article();
                article.setId(rs.getInt("aid"));
                article.setTitle(rs.getString("title"));
                reply.setArticle(article);
                list.add(reply);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return list;

    }
}
